using System;

namespace Prologis.ShifterNT.TypeConverter
{
    /// <summary>
    /// Type converter for Boolean.
    /// </summary>
    /// <remarks>
    /// Supportes conversion from string to boolean type.
    /// </remarks>
    public class BooleanConverter : IConvertFrom
    {
        #region Implementation of IConvertFrom

        /// <summary>
        /// Can the source type be converted to the type supported by this object
        /// </summary>
        /// <param name="sourceType">the type to convert</param>
        /// <returns>true if the conversion is possible</returns>
        public bool CanConvertFrom(Type sourceType)
        {
            return sourceType == typeof(string);
        }

        /// <summary>
        /// Convert the source object to the type supported by this object
        /// </summary>
        /// <param name="source">the object to convert</param>
        /// <returns>the converted object</returns>
        public object ConvertFrom(object source)
        {
            if (source is string)
            {
                return bool.Parse((string)source);
            }
            throw new Exception(source.ToString());
        }

        #endregion
    }
}
